package com.leetcode.题库;

import com.leetcode.common.model.ListNode;

/**
 * @author 覃国强
 * @date 2022-03-23 16:11
 */
public class 题库_0142_环形链表II {


  static
  public class Solution {
    public ListNode detectCycle(ListNode head) {

      ListNode slow = head;
      ListNode fast = head;
      while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
        if (slow == fast) {
          break;
        }
      }

      if (fast == null || fast.next == null) {
        return null;
      }

      while (head != slow) {
        head = head.next;
        slow = slow.next;
      }

      return head;
    }
  }

}
